package com.flyinzhen.customer.controller;

import com.flyinzhen.core.constants.ResponseStatus;
import com.flyinzhen.core.dto.ResponseDto;
import com.flyinzhen.customer.dto.input.*;
import com.flyinzhen.customer.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * 客户信息控制器
 *
 * @author yinzhen
 * @date 2019-05-01
 */
@Api
@CrossOrigin("*")
@RestController
@RequestMapping(value = "/cust", produces = "application/json;charset=UTF-8")
public class CustomerController {

    @Resource
    CustomerService customerService;

    @PostMapping("/basic")
    @ApiOperation(value = "保存基本信息", notes = "保存客户基本信息")
    public ResponseDto saveBasicInfo(@RequestBody @Validated BasicSaveInput inputDto, BindingResult validation) {
        return validation.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : customerService.saveBasicInfo(inputDto);
    }

    @GetMapping("/basic/{userId}")
    @ApiOperation(value = "获取基本信息", notes = "获取客户基本信息")
    public ResponseDto getBasicInfo(@PathVariable @NotBlank String userId) {
        return customerService.getBasicInfo(userId);
    }

    @PostMapping("/company")
    @ApiOperation(value = "保存公司信息", notes = "保存客户公司信息")
    public ResponseDto saveCompanyInfo(@RequestBody @Validated CompanySaveInput inputDto, BindingResult validation) {
        return validation.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : customerService.saveCompanyInfo(inputDto);
    }

    @GetMapping("/company/{userId}")
    @ApiOperation(value = "获取公司信息", notes = "获取公司信息")
    public ResponseDto getCompanyInfo(@PathVariable @NotBlank String userId) {
        return customerService.getCompanyInfo(userId);
    }

    @PostMapping("/id-card")
    @ApiOperation(value = "保存身份证信息", notes = "保存客户身份证信息")
    public ResponseDto saveIdCardInfo(@RequestBody @Validated IdCardSaveInput inputDto, @ApiIgnore Errors errors) {
        return errors.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : customerService.saveIdCardInfo(inputDto);
    }

    @GetMapping("/id-card/{userId}")
    @ApiOperation(value = "获取身份证信息", notes = "获取身份证信息")
    public ResponseDto getIdCardInfo(@PathVariable @NotBlank String userId) {
        return customerService.getIdCardInfo(userId);
    }

    @PostMapping("/bank-card")
    @ApiOperation(value = "保存银行卡信息", notes = "保存客户银行卡信息")
    public ResponseDto saveBankCardInfo(@RequestBody @Validated BankCardSaveInput inputDto, @ApiIgnore Errors errors) {
        return errors.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : customerService.saveBankCardInfo(inputDto);
    }

    @GetMapping("/bank-card/{userId}")
    @ApiOperation(value = "获取银行卡信息", notes = "获取客户银行卡信息")
    public ResponseDto getBankCardInfo(@PathVariable @NotBlank String userId) {
        return customerService.getBankCardInfo(userId);
    }

    @PostMapping("/contacts")
    @ApiOperation(value = "保存联系人信息", notes = "保存客户联系人信息")
    public ResponseDto saveContactsInfo(@RequestBody @Validated ContactsSaveInput inputDto, @ApiIgnore Errors errors) {
        return errors.hasErrors() ? new ResponseDto(ResponseStatus.PARAM_INVALID) : customerService.saveContactsInfo(inputDto);
    }

    @GetMapping("/contacts/{userId}")
    @ApiOperation(value = "获取联系人信息", notes = "获取客户联系人信息")
    public ResponseDto getContactsInfo(@PathVariable @NotBlank String userId) {
        return customerService.getContactsInfo(userId);
    }

    @PostMapping("/image")
    @ApiOperation(value = "保存影像信息", notes = "上传并保存客户影像信息")
    public ResponseDto saveImageInfo(@RequestParam String userId, @RequestParam String fileType, MultipartFile file,
                                     @RequestParam(required = false) String status) {
        return customerService.saveImageInfo(userId, fileType, file, status);
    }

    @GetMapping("/images/{userId}")
    @ApiOperation(value = "获取影像信息", notes = "获取客户影像信息")
    public ResponseDto getImageInfos(@PathVariable @NotBlank String userId) {
        return customerService.getImageInfos(userId);
    }
}